home *** CD-ROM | disk | FTP | other *** search
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/param.h>
- #include <fcntl.h>
- #include <errno.h>
- #include "crtlocal.h"
-
- int macstat(StringPtr path, struct stat *buf, short nVRefNum, long lDirID)
- {
- CInfoPBRec cPB;
- memset(buf, 0, sizeof(struct stat));
- cPB.hFileInfo.ioNamePtr = path;
- cPB.hFileInfo.ioVRefNum = nVRefNum;
- cPB.hFileInfo.ioDirID = lDirID;
- cPB.hFileInfo.ioFDirIndex = 0;
-
- if (PBGetCatInfoSync(&cPB))
- {
- errtran(cPB.hFileInfo.ioResult);
- return -1;
- }
-
- /* Type of file: directory or regular file + access */
-
- if (cPB.hFileInfo.ioFlAttrib & ioDirMask)
- {
- buf->st_ino = cPB.dirInfo.ioDrDirID;
- buf->st_mode = S_IFDIR|0777;
- }
- else
- {
- int siz = cPB.hFileInfo.ioFlClpSiz;
- if (!siz) siz = 8192;
- buf->st_blksize = siz;
- buf->st_blocks = (cPB.hFileInfo.ioFlPyLen+siz-1) / siz;
- buf->st_uid = cPB.hFileInfo.ioFlFndrInfo.fdCreator >> 16;
- buf->st_gid = cPB.hFileInfo.ioFlFndrInfo.fdCreator & 0xFFFF;
- buf->st_ino = cPB.hFileInfo.ioDirID;
- buf->st_size = cPB.hFileInfo.ioFlLgLen;
- buf->st_mode = S_IFREG;
- if (cPB.hFileInfo.ioFlFndrInfo.fdFlags & 0x8000)
- {
- buf->st_size = MAXPATHLEN;
- buf->st_mode = S_IFLNK;
- }
- if (cPB.hFileInfo.ioFlFndrInfo.fdType ==
- (cPB.hFileInfo.ioFlFndrInfo.fdType&0xFFFF)*0x10001)
- {
- buf->st_mode = cPB.hFileInfo.ioFlFndrInfo.fdType;
- }
- else
- buf->st_mode |= 0666;
- if (cPB.hFileInfo.ioFlAttrib & 0x01)
- {
- buf->st_mode &= ~0222;
- }
- if (cPB.hFileInfo.ioFlFndrInfo.fdFlags & fInvisible)
- {
- buf->st_mode &= ~0444;
- }
- buf->st_rdev = cPB.hFileInfo.ioFlFndrInfo.fdFldr;
- }
- /* last access time, modification time and creation time(?) */
- buf->st_atime = buf->st_mtime = unixTime(cPB.hFileInfo.ioFlMdDat);
- buf->st_ctime = unixTime(cPB.hFileInfo.ioFlCrDat);
- buf->st_dev = (long)cPB.hFileInfo.ioVRefNum;
- buf->st_nlink = 1;
- return 0;
- }
-
- int fstat(int fd, struct stat *buf)
- {
- FCBPBRec pb;
- Str255 name;
- int refnum = crt_fd_tab[fd].fd;
- if (!refnum || (crt_fd_tab[fd].flags & O_PIPE))
- {
- memset(buf, 0, sizeof(struct stat));
- buf->st_mode = S_IFIFO;
- buf->st_blksize = 8192;
- return 0;
- }
- if (crt_fd_tab[fd].flags & O_CATALOG)
- {
- FSSpec canon = getparent(crt_fd_tab[fd].fd);
- return macstat(canon.name, buf, canon.vRefNum, canon.parID);
- }
- pb.ioRefNum = refnum;
- pb.ioFCBIndx = 0;
- pb.ioCompletion = 0;
- pb.ioVRefNum = crt_ioVRefNum;
- pb.ioNamePtr = (StringPtr) name;
- PBGetFCBInfoSync(&pb);
- return macstat(name, buf, pb.ioFCBVRefNum, pb.ioFCBParID);
- }
-
- int stat(const char *name, struct stat *buf)
- {
- FSSpec canon = hfs_canon(crt_parID, name, 1);
- if (!*canon.name) return -1;
- return macstat(canon.name, buf, canon.vRefNum, canon.parID);
- }
-
- int lstat(const char *name, struct stat *buf)
- {
- FSSpec canon = hfs_canon(crt_parID, name, 0);
- if (!*canon.name) return -1;
- return macstat(canon.name, buf, canon.vRefNum, canon.parID);
- }
-
- #include <sys/unistd.h>
-
- int access(const char *name, int cmd)
- {
- int err;
- struct stat statbuf;
- FSSpec canon = hfs_canon(crt_parID, name, 1);
- if (!*canon.name) return -1;
- err = macstat(canon.name, &statbuf, canon.vRefNum, canon.parID);
- if ((cmd&R_OK) && (S_IREAD&~statbuf.st_mode)) err = -1;
- if ((cmd&W_OK) && (S_IWRITE&~statbuf.st_mode)) err = -1;
- if ((cmd&X_OK) && (S_IEXEC&~statbuf.st_mode)) err = -1;
- return err;
- }
-